/**
 * 
 */
package pl.krzosekm.vehicleregister.backup;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import org.jdesktop.application.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import pl.krzosekm.vehicleregister.model.User;
import pl.krzosekm.vehicleregister.service.UserService;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * @author michal.krzosek
 *
 */
@SuppressWarnings("serial")
public class RepairsTable extends JTable {


	
	private String[] locations = {"classpath:pl/krzosekm/vehicleregister/resources/applicationContext-service.xml"};
    private ApplicationContext ctx = new FileSystemXmlApplicationContext(locations);
	
    @Autowired
	private UserService userService = (UserService)ctx.getBean("userService");
	
		


//		public boolean isCellEditable(int rowIndex, int colIndex) { 
//			if(colIndex <= 1) 
//				return false;
//			else 
//				return true;
//		} 
//	};
	
	private void init()
	{

		List<User> users = userService.findAllUsers();
		this.setModel(new UserViewModel(users));
//		this.getColumnModel().getColumn(0).setMaxWidth(30);
//		this.getColumnModel().getColumn(0).setResizable(false);
//		this.getColumnModel().getColumn(0).setCellEditor(null);
		
		ListSelectionModel listMod =  getSelectionModel();
    	listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    	listMod.addListSelectionListener(this);

    	
    	this.addMouseListener(new MouseAdapter() {
    		public void mouseClicked(MouseEvent evt) {
    			thisMouseClicked(evt);
    		}
    	} );
		
	}

	public RepairsTable() {
		super();
		
		init();
		
		
    	
	}
	
	@Action
	private void thisMouseClicked(MouseEvent evt) {
		if (evt.getClickCount() == 2){
			 UserViewModel uvm = (UserViewModel) this.getModel();
			 User user = uvm.getUserAt(getSelectedRow());
			 System.out.println(user.getId());
	    	 RepairFrame repairFrame = new RepairFrame(HeaderPanel.EDIT);
	 		 repairFrame.setVisible(true);
	    	 
		}
	}
	
	public void setUserService(UserService userService) {
	    this.userService = userService;
	}

	public UserService getUserService() {
	    return userService;
	}
	

}
